using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using SypEngine;
using SypConst.Garbage;
using SypEngine.SiteConfig;
using SypConst.Path;
using System.Text.RegularExpressions;
using System.Web;

namespace SypEngine
{

    //SypSite
    public class SypPictureVisitor : SypVisitor
    {
        public SypPictureVisitor(SetText WriteMethod, VisitorCallBack[] Callbacks)
            : base(WriteMethod, Callbacks)
        {
        }

     

        public void VisitShowbrowserMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 1)
            {
                int SiteId = -1;
                if (int.TryParse(parameters[1], out SiteId))
                {
                    if (Callbacks.Length > 1)
                        Callbacks[1](SiteId);
                }
                else
                    OutputMethod("Showbrowser-picture : cannot convert " + SiteId + "in int type.", Output.ERROR);
            }
            else
                OutputMethod("Showbrowser-picture : missing parameters", Output.ERROR);
        }

        public void VisitAddfromdropzoneMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 3)
            {
                int SiteId = -1;
                int AlbumId = -1;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    AlbumId = int.Parse(cleanArg(parameters[2]));
                }
                catch (Exception)
                {
                    OutputMethod("Add-picture : one or more parameters are not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                if (mySite != null)
                {
                    string _files = cleanArg(parameters[3]);
                    String[] _filenames = Regex.Split(_files.Trim(), SypGarbage.COMMAND_SEPARATOR.ToString() + "+(?=(?:[^\']*\'[^\']*\')*(?![^\']*\'))");
                    SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
                    String[] pictures = new String[_filenames.Length];
                    string _desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";

                    foreach (SHDocVw.InternetExplorer e in shellWindows)
                    {
                        string explorername = Path.GetFileNameWithoutExtension(e.FullName).ToLower();

                        if (explorername.Equals("explorer"))
                        {
                            int i = 0;
                            foreach(string _filename in _filenames){

                                string _path = HttpUtility.UrlDecode(e.LocationURL).Replace("file:///", "") + "/" + cleanArg(_filename);
                                if ((pictures[i] == String.Empty || pictures[i] == null) && File.Exists(_path))
                                    pictures[i] = _path;
                                i++;
                            }
                        }
                    }

                    for (int k = 0; k < pictures.Length; k++)
                    {
                        string _path = _desktopPath + cleanArg(_filenames[k]);
                        if ((pictures[k] == String.Empty || pictures[k] == null) && File.Exists(_path))
                            pictures[k] = _path;
                    }

                    if (mySite.CategorysList != null)
                    {
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.AlbumList != null)
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    if (alb.id == AlbumId)
                                    {

                                        SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                        SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);

                                        string onlineAlbumPage = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, cat.Id, alb.id);
                                        SiteObj.AddAction(mySite.SiteId, onlineAlbumPage, SyncAction.ADD_FILE, SyncMode.ONLINE);

                                        foreach (string picturename in pictures)
                                        {
                                            if (File.Exists(picturename))
                                            {
                                                FileInfo finfo = new FileInfo(picturename);
                                                Picture newPicture = new Picture(finfo.Name, picturename);

                                                //Copy picture
                                                SiteObj.AddAction(mySite.SiteId, new string[] { picturename, newPicture.Id.ToString() }, SyncAction.COPY_PICTURE, SyncMode.LOCAL);
                                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, -1, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, -1, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);


                                                if (alb.PicturesList == null)
                                                    alb.PicturesList = new List<Picture>();
                                                alb.PicturesList.Insert(0, newPicture);
                                                mySite.AlbumIdToReload = alb.id;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }


                }
            }

        }

        public override void VisitAddMethod(SypSiteHandler SiteObj,  string[] parameters)
        {
            if (parameters.Length > 3)
            {
                int SiteId = -1;
                int AlbumId = -1;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    AlbumId = int.Parse(cleanArg(parameters[2]));
                }
                catch (Exception)
                {
                    OutputMethod("Add-picture : one or more parameters are not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                string AlbumTitle = cleanArg(parameters[3]);
                if (mySite != null)
                {
                    if (mySite.CategorysList != null)
                    {
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.AlbumList != null)
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    if (alb.id == AlbumId)
                                    {

                                        SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                        SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                        //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                        //SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef., null, -1, cat.Id, alb.id), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                        string onlineAlbumPage = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, cat.Id, alb.id);
                                        SiteObj.AddAction(mySite.SiteId, onlineAlbumPage, SyncAction.ADD_FILE, SyncMode.ONLINE);

                                        //Add album folder
                                        FileInfo finfo = new FileInfo(cleanArg(parameters[3]));
                                        Picture newPicture = new Picture(finfo.Name, parameters[3]);

                                        //Copy picture
                                        SiteObj.AddAction(mySite.SiteId, new string[] { parameters[3], newPicture.Id.ToString() } , SyncAction.COPY_PICTURE, SyncMode.LOCAL);
                                        //SiteObj.AddAction(mySite.SiteId, SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + newPicture.Id + SypGarbage.PICTURE_EXTENSION, SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, -1, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                        //SiteObj.AddAction(mySite.SiteId, SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + newPicture.Id + SypGarbage.THUMBNAIL_EXTENSION, SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, -1, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);


                                        if (alb.PicturesList == null)
                                            alb.PicturesList = new List<Picture>();
                                        alb.PicturesList.Insert(0, newPicture);
                                        mySite.AlbumIdToReload = alb.id;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                    OutputMethod("[" + SiteId + "]Add-picture : siteid " + SiteId + " not found", Output.ERROR);
            }
            else
                OutputMethod("Add-picture : missing parameters", Output.ERROR);
        }

        public override void VisitEditMethod(SypSiteHandler SiteObj,  string[] parameters)
        {
            OutputMethod("Edit-picture : The method is not implemented", Output.OUT);
        }

        public override void VisitSupprMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 2)
            {
                int SiteId = -1;
                int PictureId = -1;
                int AlbumVisitedId = -1;

                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    PictureId = int.Parse(cleanArg(parameters[2]));
                    if (parameters.Length > 3)
                        AlbumVisitedId = int.Parse(cleanArg(parameters[3]));
                }
                catch (Exception)
                {
                    OutputMethod("Suppr-picture : one or more parameters are not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                if (mySite != null)
                {
                    Picture pictToSuppr = null;
                    int albId = -1;
                    if (mySite.CategorysList != null)
                    {
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.AlbumList != null)
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    if (alb.PicturesList != null)
                                    {
                                        foreach (Picture pict in alb.PicturesList)
                                        {
                                            if (pict.Id == PictureId)
                                            {
                                                //Suppr picture file
                                                pictToSuppr = pict;
                                                albId = alb.id;
                                                
                                                
                                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.THUMBNAIL_FILE, null, mySite.SiteId, -1, -1, pict.Id), SyncAction.SUPPR_FILE, SyncMode.LOCAL);
                                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.PICTURE_FILE, null, mySite.SiteId, -1, -1, pict.Id), SyncAction.SUPPR_FILE, SyncMode.LOCAL);
                                                

                                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, -1, -1, -1, pict.Id), SyncAction.SUPPR_FILE, SyncMode.ONLINE);
                                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, -1, -1, -1, pict.Id), SyncAction.SUPPR_FILE, SyncMode.ONLINE);

                                                //suppr sync picture
                                                //mySyncSite.RemovePictureBloc(sectId, pict.originalPath);
                                            }
                                        }
                                        if (albId != -1)
                                        {
                                            SypPage EditPictureListPage = new SypPage(alb.id, SiteId,OutputMethod, mySite.Language);
                                            SiteObj.AddAction(mySite.SiteId, EditPictureListPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                            //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                            SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id, -1), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                            if (pictToSuppr != null)
                                            {
                                                alb.PicturesList.Remove(pictToSuppr);
                                            }
                                        }
                                    }


                                }
                            }
                        }
                    }
                    mySite.AlbumIdToReload = AlbumVisitedId;
                }
                else
                    OutputMethod("[" + SiteId + "]Suppr-picture : siteid " + SiteId + " not found", Output.ERROR);
            }
            else
                OutputMethod("Suppr-picture : missing parameters", Output.ERROR);


        }

    }
}
